package cn.dida.controller;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dida.model.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.dida.service.ActivityService;
import cn.dida.service.ActivityWinningService;
import cn.dida.service.BranchService;
import cn.dida.service.GeneralSetService;
import cn.dida.service.PrizeService;
import cn.dida.util.DateUtils;
import cn.dida.util.ExcelUtil;
import cn.dida.util.NumberConvetUtil;

/**
 * <p>兑奖管理</p>
 * @author lisha
 * @date 2016-11-20 19:36
 * */
@Controller
public class ActivityWinningController extends BaseController {

	@Value("${upload.path.pic}")
	private String picStoragePath;

	@Autowired
	private ActivityWinningService winService;
	@Autowired
	private PrizeService prizeService;//奖品
	@Autowired
	private BranchService branchService;//自提网点
	@Autowired
	private ActivityService activityService;//活动\\
	@Autowired
	private GeneralSetService generalSetService;


	/**
	 * 根据id获取兑奖详情
	 * @param id 兑奖记录id
	 * @return HttpResponseBodyWrapper
	 * */
	@ResponseBody
	@GetMapping("/activitywinning/{id}")
	public HttpResponseBodyWrapper getWinning(HttpServletRequest request, @PathVariable String id) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		UserSession userSession = getUserSession(request);
		ActivityWinning result = winService.getWinningById(id);
		return new HttpResponseBodyWrapper(result);
	}

	/**
	 * 点击发送: 修改获奖列表，修改红包获奖日志表， 修改奖品库存
	 * @param winningId 获奖记录id
	 * @param prizeId 奖品ID
	 * @return
	 */
	@ResponseBody
	@PostMapping("/sendEnvelope")
	public HttpResponseBodyWrapper sendEnvelope(HttpServletRequest request, Integer winningId,Integer prizeId) throws Exception {
		UserSession userSession = getUserSession(request);
		Map<String, Object> genmap = generalSetService.queryGeneralSet(userSession.getCompanyId());
		Integer source = null;
		if(genmap.containsKey("setupvalue")){
			String setupvalue = (String) genmap.get("setupvalue");
			if("redpack".equals(setupvalue.trim())){
				source = 0;
			}else{
				source = 1;
			}
		}else{
			source = 0;
		}
		winService.sendEnvelope(winningId, prizeId,source,userSession.getCompanyId());
		return new HttpResponseBodyWrapper(true);
	}

	/**
	 * 红包补发数据
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/getSendEvnFalse")
	public String qrcode_download(HttpServletRequest request, HttpServletResponse response,String activityId)
			throws IOException {

		UserSession userSession = getUserSession(request);

		String fileName = "红包补发数据";

		Integer companyId = userSession.getCompanyId();

//		String tmpfilefullname =DateUtils.dateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd") + fileName + ".txt";
		String tmpfilefullname = DateUtils.dateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd") + fileName + ".txt";

//		File temf = new File(tmpfilefullname);
//
//			if (!temf.getParentFile().exists()) {
//				temf.getParentFile().mkdirs();
//			}
//			temf.createNewFile();

		List<Map<String, Object>> list = winService.getSendEvnFalse(companyId,activityId);

		FileWriter fw = null;
		BufferedWriter writer = null;

		try {
			fw = new FileWriter(tmpfilefullname);
			writer = new BufferedWriter(fw);

			//获取公司uuid
			String companyUuid = winService.getCompanyApid(companyId);
			if(companyUuid == null){
				companyUuid ="未获取到公司公众号信息";
			}
			writer.write(companyUuid);
			writer.write("\r\n");// 换行

			for (Map<String, Object> map : list) {

				if(map.get("amount")==null || map.get("openid") == null){
					continue;
				}
				BigDecimal b = new BigDecimal(100);
				BigDecimal price = (BigDecimal)map.get("amount");
				writer.write(map.get("openid") + " " + price.divide(b,2,BigDecimal.ROUND_HALF_EVEN));
				writer.write("\r\n");// 换行
			}

			writer.flush();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		InputStream is = new FileInputStream(tmpfilefullname);
		// OutputStream out = response.getOutputStream();
		// ByteArrayOutputStream os = new ByteArrayOutputStream();
		// byte[] content = os.toByteArray();
		// InputStream is = new ByteArrayInputStream(content);
		// 设置response参数，可以打开下载页面
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String((fileName + ".txt").getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
				bos.flush();
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}

		return null;
	}


	/**
	 * 兑奖列表
	 * @param qrcodeNum 二维码编号
	 * @param qrcodeName 二维码名称
	 * @param productName 商品名称
	 * @param prizeName 奖品名称
	 * @param csshMobile 兑奖手机号
	 * @param cashTimeS 兑奖时间
	 * @param cashTimeE 兑奖时间
	 * @param scanTimeS 兑奖时间
	 * @param scanTimeS 兑奖时间
	 * @param page 分页游标
	 * @return ModelAndView
	 * */
	@GetMapping("/activitywinning")
	public ModelAndView list(HttpServletRequest request, String qrcodeNum,
							 String qrcodeName, String productName, String prizeName,
							 String csshMobile, String cashTimeS, String cashTimeE, String scanTimeS,
							 String scanTimeE, Integer redEnvelope,String cashTime,Integer page,String id,Integer shipMethod,String activityId) throws Exception {

		Map<String, Object> model = new HashMap<String, Object>();
		UserSession userSession = getUserSession(request);

		page = (page == null || page < 1) ? 1 : page;

		prizeName = StringUtils.trimToEmpty(prizeName);
		qrcodeNum = StringUtils.trimToEmpty(qrcodeNum);
		qrcodeName = StringUtils.trimToEmpty(qrcodeName);
		productName = StringUtils.trimToEmpty(productName);
		csshMobile = StringUtils.trimToEmpty(csshMobile);
		if(qrcodeName != null){
			model.put("qrcodeName", qrcodeName);
		}
		if(productName != null){
			model.put("productName", productName);
		}
		if(prizeName != null){
			model.put("prizeName", prizeName);
		}

		model.put("qrcodeNum", qrcodeNum);
		model.put("csshMobile", csshMobile);
		model.put("scanTimeS", scanTimeS);
		model.put("scanTimeE", scanTimeE);
		model.put("cashTimeS", cashTimeS);
		model.put("cashTimeE", cashTimeE);
		model.put("cashTime", cashTime);
		model.put("shipMethod", shipMethod);
		model.put("activityId", activityId);
		model.put("redEnvelope", redEnvelope);


		if(StringUtils.isNotBlank(id)){
			ActivityWinning winning = winService.getWinningById(id);
			String showCode = NumberConvetUtil.baseString(NumberUtils.toLong(winning.getQrcodeId().toString(), 0L),
					NumberConvetUtil.SIXTY_CONVET);
			winning.setShowCode(showCode);
			model.put("winning", winning);


			//根据奖品id获取奖品信息
			if(winning.getPrizeId() != null){
				Prize prize = prizeService.getPrizeById(winning.getPrizeId(),userSession.getCompanyId());
				model.put("prize", prize);
			}else{
				model.put("prize", new Prize());
			}

			//根据自提网点id获取网点信息
			if(winning.getStoreId() != null){
				Branch branch = branchService.getBranch(userSession.getCompanyId(), winning.getStoreId());
				model.put("branch", branch);
			}else{
				model.put("branch", new Branch());
			}

			//根据活动id获取活动信息
			if(winning.getActivityId() != null){
				Activity activity = activityService.getActivityById(String.valueOf(winning.getActivityId()));
				String selectedRgn = activityService.getRgnByActivityId(winning.getActivityId().toString());
				activity.setAreaName(selectedRgn);
				model.put("activity", activity);
			}else{
				model.put("activity", new Activity());
			}


			//根据兑奖记录id获取配送信息
			DeliveryInfo info = winService.getDeliveryInfo(id);
			if(info == null){
				model.put("info", new DeliveryInfo());
			}else{
				model.put("info", info);
			}

			//根据兑奖记录中的activity_prize_id获取奖品等级
			ActivityPrize ap = activityService.getActivityPrizeByapId(winning.getActivityPrizeId().toString());
			if(ap == null){
				model.put("ap", new ActivityPrize());
			}else{
				model.put("ap", ap);
			}

			return new ModelAndView("winning_detail").addAllObjects(model);

		}else{
			if(StringUtils.isNotBlank(scanTimeS)){
				scanTimeS = String.valueOf(DateUtils.stringToLong(scanTimeS, "yyyy-MM-dd HH:mm:ss"));
			}
			if(StringUtils.isNotBlank(scanTimeE)){
				scanTimeE = String.valueOf(DateUtils.stringToLong(scanTimeE, "yyyy-MM-dd HH:mm:ss"));
			}
			if(StringUtils.isNotBlank(cashTimeS)){
				cashTimeS = String.valueOf(DateUtils.stringToLong(cashTimeS, "yyyy-MM-dd HH:mm:ss"));
			}
			if(StringUtils.isNotBlank(cashTimeE)){
				cashTimeE = String.valueOf(DateUtils.stringToLong(cashTimeE, "yyyy-MM-dd HH:mm:ss"));
			}

			Pageable<ActivityWinning> winningList = winService.queryWinning(userSession.getCompanyId(),qrcodeNum, qrcodeName, productName, prizeName,
					csshMobile, cashTimeS, cashTimeE, scanTimeS, scanTimeE, page,shipMethod,activityId, redEnvelope);
			model.put("winningList", winningList);
			return new ModelAndView("winning_list").addAllObjects(model);
		}
	}
	/**
	 * 兑奖列表导出
	 * @param
	 * @param qrcodeNum 二维码编号
	 * @param qrcodeName 二维码名称
	 * @param productName 商品名称
	 * @param prizeName 奖品名称
	 * @param csshMobile 手机号
	 * @param cashTimeS 兑奖时间
	 * @param cashTimeE 兑奖时间
	 * @param scanTimeS 兑奖时间
	 * @param scanTimeS 兑奖时间
	 * @return ModelAndView
	 * */
	@GetMapping("/activitywinning/export")
	public ModelAndView activitywinning_export(HttpServletRequest request, HttpServletResponse response, String qrcodeNum,
											   String qrcodeName, String productName, String prizeName,
											   String csshMobile, String cashTimeS, String cashTimeE, String scanTimeS,
											   String scanTimeE, Integer shipMethod,Integer redEnvelope,String activityId) throws Exception {
		UserSession userSession = getUserSession(request);

		if(StringUtils.isNotBlank(scanTimeS)){
			scanTimeS = String.valueOf(DateUtils.stringToLong(scanTimeS, "yyyy-MM-dd HH:mm:ss"));
		}
		if(StringUtils.isNotBlank(scanTimeE)){
			scanTimeE = String.valueOf(DateUtils.stringToLong(scanTimeE, "yyyy-MM-dd HH:mm:ss"));
		}
		if(StringUtils.isNotBlank(cashTimeS)){
			cashTimeS = String.valueOf(DateUtils.stringToLong(cashTimeS, "yyyy-MM-dd HH:mm:ss"));
		}
		if(StringUtils.isNotBlank(cashTimeE)){
			cashTimeE = String.valueOf(DateUtils.stringToLong(cashTimeE, "yyyy-MM-dd HH:mm:ss"));
		}


		List<ActivityWinning> exportList = winService.queryWinningForExport(userSession.getCompanyId(),activityId, qrcodeNum,
				qrcodeName, productName, prizeName, csshMobile, cashTimeS, cashTimeE, scanTimeS, scanTimeE, shipMethod,redEnvelope);


		String fileName="兑奖记录数据导出"+DateUtils.dateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd");

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>(exportList.size());

		Map<String,Object> fmap = null;
		for(ActivityWinning obj : exportList){
			fmap=new HashMap<String, Object>();
			Long tid = NumberUtils.toLong(obj.getId().toString(), 0L);
			fmap.put("id", tid);
			fmap.put("qrcodeId", obj.getQrcodeId());
			if(obj.getQrcodeType() ==0){
				fmap.put("qrcodeType", "组合码瓶码");
			}else if(obj.getQrcodeType() == 1){
				fmap.put("qrcodeType", "组合码箱码");
			}else if(obj.getQrcodeType() == 2){
				fmap.put("qrcodeType", "普通码瓶码");
			}else if(obj.getQrcodeType() == 3){
				fmap.put("qrcodeType", "普通码箱码");
			}
			fmap.put("qrcodeName", obj.getQrcodeName());
			fmap.put("productName", obj.getProductName());
			fmap.put("scanTime", DateUtils.formatTimeInMillis(obj.getScanTime()));
			fmap.put("prizeName", obj.getPrizeName());
			fmap.put("prizePrice", obj.getPrizePrice());
			fmap.put("weixinOpenid",obj.getWeixinOpenid());

			if(obj.getShipMethod() != null){
				if(Integer.valueOf(obj.getShipMethod()) == 1){
					fmap.put("shipMethod", "自动发送");
				}else if(Integer.valueOf(obj.getShipMethod()) == 2){
					fmap.put("shipMethod", "上门自提");
				}else if(Integer.valueOf(obj.getShipMethod()) == 3){
					fmap.put("shipMethod", "物流配送");
				}
			}else{
				fmap.put("shipMethod", "");
			}

			fmap.put("csshMobile", obj.getCsshMobile());
			fmap.put("consignee", obj.getConsignee());
			fmap.put("mobile", obj.getMobile());
			fmap.put("adress", obj.getAdress());
			Integer redEnvIsSuc = obj.getRedEnvIsSuc();
			if( redEnvIsSuc!= null){
				if(redEnvIsSuc == 1){
					fmap.put("redEnvIsSuc", "发送成功");
				}else if(redEnvIsSuc == 2){
					fmap.put("redEnvIsSuc", "手动发送成功");
				}else if(redEnvIsSuc == 0){
					fmap.put("redEnvIsSuc", "发送失败");
				}

				if(redEnvIsSuc==0 || redEnvIsSuc ==2){
					fmap.put("redEnvErrorDes", obj.getRedEnvErrorDes());
				}
			}else{
				fmap.put("redEnvIsSuc", "");
				fmap.put("redEnvErrorDes", "");
			}
			if(obj.getCashTime() != null){
				fmap.put("cashTime", DateUtils.formatTimeInMillis(obj.getCashTime()));
			}else{
				fmap.put("cashTime", "");
			}
			fmap.put("poiCity", obj.getPoiCity());
			fmap.put("poiAddress", obj.getPoiAddress());


			list.add(fmap);
		}

		String columnNames[] = {"ID","二维码编号","二维码类型","二维码批次","活动商品","扫码时间","奖品名称","奖品价格","配送方式","兑奖手机号","收件人","联系电话","配送地址","红包发送","失败原因","兑奖时间","weixinOpenid","位置", "详细地址"};//列名
		String keys[] = {"id","qrcodeId","qrcodeType","qrcodeName","productName","scanTime","prizeName","prizePrice","shipMethod","csshMobile","consignee","mobile","adress","redEnvIsSuc","redEnvErrorDes","cashTime","weixinOpenid","poiCity","poiAddress"};//map中的key
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ExcelUtil.createWorkBook(list,keys,columnNames).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		// 设置response参数，可以打开下载页面
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
				bos.flush();
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}

		return null;
	}
	/**
	 * 获取兑奖记录
	 * @param activityId 活动id
	 * @param qrcodeNum 二维码编号
	 * @param qrcodeName 二维码名称
	 * @param productName 商品名称
	 * @param prizeName 奖品名称
	 * @param mobile 兑奖手机号
	 * @param cashTimeS 兑奖时间
	 * @param cashTimeE 兑奖时间
	 * @param scanTimeS 兑奖时间
	 * @param scanTimeS 兑奖时间
	 * @param page 分页游标
	 * @return ModelAndView
	 * */
	@GetMapping("/activity/getActivityWinningList")
	public ModelAndView getListByActivityId(HttpServletRequest request, String activityId,String qrcodeNum,
											String qrcodeName, String productName, String prizeName,
											String mobile, String cashTimeS, String cashTimeE, String scanTimeS,
											String scanTimeE, String scanTime,String cashTime,Integer page,String id ,Integer shipMethod) throws Exception {

		Map<String, Object> model = new HashMap<String, Object>();
		UserSession userSession = getUserSession(request);

		page = (page == null || page < 1) ? 1 : page;

		prizeName = StringUtils.trimToEmpty(prizeName);
		qrcodeNum = StringUtils.trimToEmpty(qrcodeNum);
		qrcodeName = StringUtils.trimToEmpty(qrcodeName);
		productName = StringUtils.trimToEmpty(productName);
		mobile = StringUtils.trimToEmpty(mobile);
		if(qrcodeName != null){
			//qrcodeName = new String(qrcodeName.getBytes("iso-8859-1"),"utf-8");//查询条件中文转码
			model.put("qrcodeName", qrcodeName);
		}
		if(productName != null){
			//productName = new String(productName.getBytes("iso-8859-1"),"utf-8");//查询条件中文转码
			model.put("productName", productName);
		}
		if(prizeName != null){
			//prizeName = new String(prizeName.getBytes("iso-8859-1"),"utf-8");//查询条件中文转码
			model.put("prizeName", prizeName);
		}

		model.put("qrcodeNum", qrcodeNum);
		model.put("mobile", mobile);
		model.put("scanTimeS", scanTimeS);
		model.put("scanTimeE", scanTimeE);
		model.put("cashTimeS", cashTimeS);
		model.put("cashTimeE", cashTimeE);
		model.put("shipMethod", shipMethod);


		if(StringUtils.isNotBlank(id)){
			ActivityWinning winning = winService.getWinningById(id);
			String showCode = NumberConvetUtil.baseString(NumberUtils.toLong(winning.getQrcodeId().toString(), 0L),
					NumberConvetUtil.SIXTY_CONVET);
			winning.setShowCode(showCode);
			model.put("winning", winning);


			//根据奖品id获取奖品信息
			if(winning.getPrizeId() != null){
				Prize prize = prizeService.getPrizeById(winning.getPrizeId(),userSession.getCompanyId());
				model.put("prize", prize);
			}else{
				model.put("prize", new Prize());
			}

			//根据自提网点id获取网点信息
			if(winning.getStoreId() != null){
				Branch branch = branchService.getBranch(userSession.getCompanyId(), winning.getStoreId());
				model.put("branch", branch);
			}else{
				model.put("branch", new Branch());
			}

			//根据活动id获取活动信息
			if(winning.getActivityId() != null){
				Activity activity = activityService.getActivityById(String.valueOf(winning.getActivityId()));
				String selectedRgn = activityService.getRgnByActivityId(winning.getActivityId().toString());
				activity.setAreaName(selectedRgn);
				model.put("activity", activity);
			}else{
				model.put("activity", new Activity());
			}


			//根据兑奖记录id获取配送信息
			DeliveryInfo info = winService.getDeliveryInfo(id);
			if(info == null){
				model.put("info", new DeliveryInfo());
			}else{
				model.put("info", info);
			}

			//根据兑奖记录中的activity_prize_id获取奖品等级
			ActivityPrize ap = activityService.getActivityPrizeByapId(winning.getActivityPrizeId().toString());
			if(ap == null){
				model.put("ap", new ActivityPrize());
			}else{
				model.put("ap", ap);
			}

			return new ModelAndView("winning_detail").addAllObjects(model);

		}else{
			if(StringUtils.isNotBlank(scanTimeS)){
				scanTimeS = String.valueOf(DateUtils.stringToLong(scanTimeS, "yyyy-MM-dd HH:mm:ss"));
			}
			if(StringUtils.isNotBlank(scanTimeE)){
				scanTimeE = String.valueOf(DateUtils.stringToLong(scanTimeE, "yyyy-MM-dd HH:mm:ss"));
			}
			if(StringUtils.isNotBlank(cashTimeS)){
				cashTimeS = String.valueOf(DateUtils.stringToLong(cashTimeS, "yyyy-MM-dd HH:mm:ss"));
			}
			if(StringUtils.isNotBlank(cashTimeE)){
				cashTimeE = String.valueOf(DateUtils.stringToLong(cashTimeE, "yyyy-MM-dd HH:mm:ss"));
			}
			Pageable<ActivityWinning> winningList = winService.queryWinningList(activityId, userSession.getCompanyId(),
					qrcodeNum, qrcodeName, productName, prizeName, mobile, cashTimeS, cashTimeE, scanTimeS,
					scanTimeE, page, shipMethod);
			model.put("winningList", winningList);
			model.put("activityId", activityId);


			return new ModelAndView("activity_winning_list").addAllObjects(model);
		}
	}

	/**
	 * 统计报表数据
	 * @param request
	 * @param response
	 * @param activityId
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/activitywinning/tatisticalForm")
	public ModelAndView statisticalFormList(HttpServletRequest request, HttpServletResponse response, String activityId) throws Exception {
		UserSession userSession = getUserSession(request);
		List<Map<String, Object>> staList = winService.statisticalFormList(userSession.getCompanyId(), activityId);
		String fileName = "统计报表数据导出" + DateUtils.dateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd");

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(staList.size());

		Map<String, Object> fmap = null;
		for (int i = 0; i < staList.size(); i++) {
			Map<String, Object> obj = staList.get(i);
			fmap = new HashMap<String, Object>();
			fmap.put("index", i + 1);
			fmap.put("proName", obj.get("proName"));
			fmap.put("proNumber", obj.get("proNumber"));
			fmap.put("actName", obj.get("actName"));

			String actTimeSta = DateUtils.transferLongToDate("yyyy-MM-dd", (Long)obj.get("actTimeSta"));
			fmap.put("actTimeSta", actTimeSta);
			String actTimeEnd = DateUtils.transferLongToDate("yyyy-MM-dd", (Long)obj.get("actTimeEnd"));
			fmap.put("actTimeEnd", actTimeEnd);
			fmap.put("priName", obj.get("priName")); //奖品名称
			fmap.put("priCount", obj.get("priCount"));//中奖数量
			fmap.put("noAward", obj.get("noAward")); //未领奖数量
			fmap.put("scanCount", obj.get("scanCount"));//扫码总量
			fmap.put("proQr", obj.get("proQr"));//绑定数量
			list.add(fmap);
		}

		String columnNames[] = {"序号", "产品名称", "产品编码", "活动名称", "活动启用时间", "活动停用时间", "未领奖", "绑码数量", "总扫码量", "奖品名称", "获奖数量"};//列名
		String keys[] = {"index", "proName", "proNumber", "actName", "actTimeSta", "actTimeEnd", "noAward", "proQr", "scanCount", "priName", "priCount"};//map中的key
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ExcelUtil.createWorkBook(list, keys, columnNames).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		// 设置response参数，可以打开下载页面
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
				bos.flush();
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
		return null;

	}


}
